package threadlocal;

  import cn.hutool.cron.task.RunnableTask;
  import com.alibaba.ttl.TransmittableThreadLocal;
  import com.alibaba.ttl.TtlRunnable;
  import com.alibaba.ttl.threadpool.TtlExecutors;

  import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description:
 * @date: 2024/2/22
 **/
public class InheritableThreadLocalTest {


    private static ExecutorService executorService = Executors.newFixedThreadPool(2);

    public static void main(String[] args) {
//        test01();
//        inheritableThreadLocalErrorTest();
//        testTransmittableThread();

        testTransmittableThreadForPool();
    }

    private static void test01() {
        //定义一个InheritableThreadLocal变量，并在子线程中输出
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        inheritableThreadLocal.set("父线程");
        inheritableThreadLocal.set("父线程2");

        Thread thread = new Thread(() -> {
            System.out.println("子线程输出：" + inheritableThreadLocal.get());
        });
        thread.start();
    }

    public static void inheritableThreadLocalErrorTest() {
        String s = "1";
        //线程池中使用inheritableThreadLocal
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        //创建一个循环，在循环中给inheritableThreadLocal变量赋值，使用线程池中的线程，输出inheritableThreadLocal变量的内容
        for (int i = 0; i < 10; i++) {
            inheritableThreadLocal.set("序号" + i);
            executorService.execute(() -> {
                System.out.println("线程池中线程输出：" + inheritableThreadLocal.get());
                inheritableThreadLocal.remove();
                System.out.println(s);
            });
        }
        executorService.shutdown();
    }

    /**
     * 测试使用transmittableThread
     */
    public static void testTransmittableThread() {

        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();


        for (int i = 0; i < 10; i++) {
            context.set(String.valueOf(i));
            inheritableThreadLocal.set(String.valueOf(i));
            TtlRunnable ttlRunnable = TtlRunnable.get(new Thread(() -> {
                System.out.println(context.get()+"-" +inheritableThreadLocal.get()) ;

            }));
            executorService.submit(ttlRunnable);
        }

        executorService.shutdown();
    }

    public static void testTransmittableThreadForPool() {

        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
        ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(executorService);

        for (int i = 0; i < 10; i++) {
            context.set(i+"");
            ttlExecutorService.execute(() -> {
                System.out.println("线程池中线程输出：" + context.get());
                context.remove();
            });
        }
        ttlExecutorService.shutdown();

    }


    public static void testAdd(int i) {

        System.out.println(i);
    }


}
